
package cz.results.winter6.oserver;


import com.db4o.*;
import com.db4o.messaging.*;
import java.util.Iterator;

public class DBClient {
    
    private final static String HOST="192.168.0.177";
    private final static int PORT=8732;
    private final static String USER="results";
    private final static String PSWD="1234";
    
    private static ObjectContainer dbClient = null;
    
    public static ObjectContainer dbGetClient() throws  Exception {
        if (dbClient == null)
            try {
                dbClient = Db4o.openClient(HOST, PORT, USER, PSWD);
            } catch (Exception e) {
                System.out.println("DBClient: Nepodarilo se pripojit k serveru");
                dbCloseClient();
            }
        return dbClient;
    }
    
    public static void dbCloseClient() {
        if (dbClient != null) {
            dbClient.close();
            dbClient = null;
        }
        
    }
    
    public static void dbStore(Object ob) throws Exception {
        try {
            dbGetClient().set(ob);
            dbGetClient().commit();
        } catch (Exception e) {
            System.out.println("DBClient: Nepodarilo se ulozit objekt.");
        }
    }
    
    public static ObjectSet dbGet(Object ob) throws Exception {
        try {
            return dbGetClient().get(ob);
        } catch (Exception e) {
            System.out.println("DBClient: Nepodarilo se ziskat objekt.");
            return null;
        }
    }
    
    public static void dbDelete(Object ob) throws Exception {
        try {
            dbGetClient().delete(ob);
            dbGetClient().commit();            
        } catch (Exception e) {
            System.out.println("DBClient: Nepodarilo se smazat objekt.");
        }
    }
    
    
    public static void dbDeleteSet(ObjectSet os) throws Exception {
        try {
            for (Iterator i = os.iterator(); i.hasNext();) {
                dbDelete(i.next());
            }
        } catch (Exception ex) {
            System.out.println("DBClient: Nepodarilo se smazat mnozinu zaznamu.");
        }
    }
    
    public static void dbZap(Object ob) throws Exception {
        try {
            ObjectSet os = dbGet(ob);
            dbDeleteSet(os);
        } catch (Exception ex) {
            System.out.println("DBClient: Nepodarilo se smazat skupinu objektu stejneho typu.");
        }
    }
    
    public static void dbZapFile() throws Exception {
        try {
            dbZap(new Object());
        } catch (Exception ex) {
            System.out.println("DBClient: Nepodarilo se nainicializovat YAP soubor.");
        }
    }
    
    public static int dbReccount(Object ob) throws Exception {
        try {
            return dbGet(ob).size();
        } catch (Exception ex) {
            System.out.println("DBClient: Nepodarilo se zjistit pocet zaznamu.");
            return -1;
        }
    }
    
    public static void dbSetConfig() {
        // Default konfigurace - spolecna pro server i klinety
        DefaultConfig.setDefaultConfig();
        
        // Individualni konfigurace klienta
        Db4o.configure().messageLevel(2);
    }
    
    public static void dbDefragment(String message) throws Exception {
        try {
            MessageSender messageSender = dbGetClient().ext().configure().getMessageSender();
            messageSender.send(new DefragmentServer(message));
        } catch (Exception ex) {
            System.out.println("DBClient: Defragmentace serveru se nezdarila.");
        }
    }
    
    public static void dbShutServer(String message) throws Exception {
        try {
            MessageSender messageSender = dbGetClient().ext().configure().getMessageSender();
            messageSender.send(new StopServer(message));
        } catch (Exception e) {
            System.out.println("DBClient: uzavreni serveru se nezdarilo.");
        }
    }
    
}